﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Net.Sockets;


namespace ClienteEvalConsole
{
    class Program
    {
        static void Main (string [] args)
        {
            TcpClient tc = new TcpClient();

            tc.Connect ("127.0.0.1", 9001);

            using (NetworkStream ns = tc.GetStream())
            {
                mandar ( ns,
                    @"
                        var fs = require ('fs');

                        fs.appendFile ('archivilo.txt', 'datos \r\n', function (err) {
                            if (err) {
                                sock.write ('hubo un error al escribir el archivete!');
                            } else {
                                sock.write  ('ok!');
                                sock.destroy();
                            }
                        });
                        #
                    "
                );

                ns.Flush();

                int @byte = 0;

                while (-1 != (@byte = ns.ReadByte()))
                {
                    Console.Write((char)@byte);
                }
                
                ns.Close();
            }
        }

        static void mandar (NetworkStream ns, string s)
        { 
            byte [] buff = Encoding.ASCII.GetBytes(s);
            ns.Write(buff, 0, buff.Length);
        }
    }
}
